package com.kye.express.business.common_tool_new;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.kye.model.bean.common_tool.CommonToolItemBean;

import java.util.Collections;
import java.util.List;

import me.drakeet.multitype.MultiTypeAdapter;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 09/01/2018
 * @updated : 09/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class CommonToolItemDragHelperCallback extends ItemTouchHelper.Callback {

    private MultiTypeAdapter adapter;
    private List<CommonToolItemBean> dataList;
    private CommonToolContract.CommonToolPresenter presenter;
    private boolean isCanDrag = true;

    public CommonToolItemDragHelperCallback(MultiTypeAdapter adapter, List<CommonToolItemBean> dataList, CommonToolContract.CommonToolPresenter presenter) {
        this.adapter = adapter;
        this.dataList = dataList;
        this.presenter = presenter;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlag;
        int swipeFlag;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            dragFlag = ItemTouchHelper.DOWN
                    | ItemTouchHelper.UP
                    | ItemTouchHelper.LEFT
                    | ItemTouchHelper.RIGHT;
            swipeFlag = 0;
        } else {
            dragFlag = ItemTouchHelper.DOWN
                    | ItemTouchHelper.UP;
            swipeFlag = ItemTouchHelper.END;
        }
        return makeMovementFlags(dragFlag, swipeFlag);
    }

    int fromPosition;
    int toPosition;

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        fromPosition = viewHolder.getAdapterPosition();
        toPosition = target.getAdapterPosition();
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(dataList, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(dataList, i, i - 1);
            }
        }
        recyclerView.getAdapter().notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        int position = viewHolder.getAdapterPosition();
        if (direction == ItemTouchHelper.END) {
            dataList.remove(position);
            adapter.notifyItemRemoved(position);
        }
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return super.isItemViewSwipeEnabled();
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return isCanDrag;
    }

    public boolean isCanDrag() {
        return isCanDrag;
    }

    public void setCanDrag(boolean canDrag) {
        isCanDrag = canDrag;
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
    }

    public void setDataList(List<CommonToolItemBean> dataList) {
        this.dataList = dataList;
    }
}
